//
//  XLGDownloadManager.h
//  Etion
//
//  Created by  user on 11-3-22.
//  Copyright 2011 GuangZhouXuanWu. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "XLGDownloadIndex.h"

#import "XLGDownloadOperation.h"

#import "XLGDownloadEnum.h"

extern NSString *const XLGDownloadManagerBackgroundMaintainKey;

extern NSString *const XLGDownloadDBFile;

extern NSString *const XLGDownloadDataFolder;

@class XLGDownloadManager;

@protocol XLGDownloadManagerDelegate <NSObject>

- (void)downloadManager:(XLGDownloadManager*)downloadManager exchangeDownloadIndex:(XLGDownloadIndex *)downloadIndex;

@end

//下载完成时会默认发送此消息
extern NSString *const XLGDownloadManagerDownloadFinished;

@interface XLGDownloadManager : NSObject <XLGDownloadOperationDelegate>
{
}

@property(nonatomic, assign) NSUInteger maxDownloadingIndex;

@property(nonatomic, readonly) NSUInteger downloadingCount;

- (void)addDownloadManagerDelegate:(id<XLGDownloadManagerDelegate>)delegate;

- (void)removeDownloadManagerDelegate:(id<XLGDownloadManagerDelegate>)delegate;


- (void)addDownloaingOperationDelegate:(id<XLGDownloadOperationDelegate>)delegate;

- (void)removeDownloadingOperationDelegate:(id<XLGDownloadOperationDelegate>)delegate;

//key:URL value:operationDelegate
- (void)removeDownloaingOperationDelegate:(NSDictionary*)dictionary;

- (void)removeDownloaingOperationDelegateWithURLs:(NSArray *)arUrl;

- (void)removeDownloadingAllOperationDelegate;

- (void)addDownloadTaskWithSourceType:(EDownloadSourceType)sourcetype taskName:(NSString *)taskName URL:(NSString *)szUrl operationDelegate:(id<XLGDownloadOperationDelegate>)delegate asyncCompletion:(void (^)(NSInteger nResult))completion;

- (void)addDownloadTaskWithTaskName:(NSString *)taskName URL:(NSString *)szUrl operationDelegate:(id <XLGDownloadOperationDelegate>)delegate asyncCompletion:(void (^)(NSInteger nResult))completion;

- (void)addDownloadTaskWithIndex:(XLGDownloadIndex *)mi operationDelegate:(id <XLGDownloadOperationDelegate>)target asyncCompletion:(void (^)(NSInteger nResult))completion;

- (void)addDownloadTaskWithOperationDelegate:(id <XLGDownloadOperationDelegate>)target index:(void (^)(XLGDownloadIndex *mi))mi asyncCompletion:(void (^)(NSInteger nResult))completion;



//- (void)AddDownloadIndexByFsfile:(CFsfilesObj *)fsfile target:(id <XLGDownloadOperationDelegate>)target complete:(void (^)(int nResult))complete;



- (void)deleteDownloadItemWithURLs:(NSArray *)arURL asyncCompletion:(void(^)(NSArray *arResult))completion;


- (void)deleteDownloadItemWithExtendIDs:(NSArray *)arExtendids asyncCompletion:(void(^)(NSArray *arResult))completion;



- (void)deleteFinishedItemWithExtendIDs:(NSArray *)arExtendids asyncCompletion:(void(^)(NSArray *arResult))completion;

- (void)deleteFinishedItemWithURLs:(NSArray *)arUrl asyncCompletion:(void(^)(NSArray *arResult))completion;



//停止下载，target!=nil，只移除移除指定界面进度delegate；target==nil，移除移除所有界面进度delegate
- (void)stopDownloadItemWithURLs:(NSArray *)arURL operationDelegate:(id <XLGDownloadOperationDelegate>)target asyncCompletion:(void(^)(NSArray *arResult))completion;

- (void)resumeDownloadWithURLs:(NSArray *)arUrl operationDelegate:(id<XLGDownloadOperationDelegate>)operationDelegate asyncCompletion:(void(^)(NSArray *arResult))completion;

- (void)redownloadFinishedTaskWithURLs:(NSArray *)arUrl operationDelegate:(id<XLGDownloadOperationDelegate>)operationDelegate asyncCompletion:(void(^)(NSArray *arResult))completion;


//枚举下载队列中的非下载完成任务
- (void)enumDownloadingItem:(void (^)(NSUInteger nIndex, XLGDownloadIndex *downloadingItem, BOOL *bStop))downloadingItem;

//枚举与DownloadManager关联的UI
//- (void)enumDownloadManagerOperationDelegate:(void (^)(NSUInteger nIndex, id obj, BOOL *bStop))downloaduitarget;


- (void)getIndex:(XLGDownloadTargetType)targetType withSourceType:(NSUInteger)sourceType completion:(void (^)(NSInteger nResult, NSArray *index))completion;

- (void)getFinishedIndexsAsyncCompletion:(void(^)(NSInteger nResult,NSArray *arIndex))completion;


//- (int)isDownloadIndexExist:(XLGDownloadTargetType)type;


- (BOOL)openDownloadFinishedFile:(NSString *)fileName withTitle:(NSString*)title presentedViewController:(UIViewController *)vcl;



//- (NSString *)FsfileToMessageInstanceObjContent:(CFsfilesObj *)fsfile;

//- (void)DownloadEndCategoryOpeation:(CMessageInstanceObj *)mi Type:(EDownloadEndType)type;

- (NSString *)defaultStorePath;


+ (void)initDownloadManager;

+ (XLGDownloadManager *)downloadManager;

+ (void)releaseDownloadManager;

+ (void)suspendDownloadManager;

+ (BOOL)resumeDownloadManger;


@end

@interface XLGDownloadManager (TransferURL)

+(void)operateNeedTransferURLBlock:(NSString*(^)(NSString *URL))transferURL;

@end
